package cn.geracomium.action;

import cn.geracomium.util.action.AbstractAction;
import cn.geracomium.util.web.SplitPageUtil;
import cn.gov.service.ICityAndProvinceService;
import cn.gov.service.IServiceStationService;
import cn.gov.vo.ServiceStation;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by {chen chao} on 2018/06/24.
 */
@Controller
public class ServiceStationAction extends AbstractAction{


    @Reference
    private IServiceStationService service ;
    @Reference
    private ICityAndProvinceService PCService;



    @RequestMapping("getServiceStation")
    @ResponseBody
    public Object getCity(String provinceName,String cityName,String areaName) {
        SplitPageUtil spu = new SplitPageUtil("雇员姓名:ename|联系电话:phone|是否离职(0：离职，1：在职):state", "/list") ;
        return this.service.getSplit(spu.getCurrentPage(),spu.getLineSize(),null,null);
    }
    @RequestMapping("city")
    @ResponseBody
    public Object getCity1(String province) {
        return this.PCService.getCityByProvince(province);
    }
    @RequestMapping("area")
    @ResponseBody
    public Object getArea(String province,String city) {

        return this.PCService.getAreaByPc(province,city);
    }
    @RequestMapping("search")
    @ResponseBody
    public Object get(String province,String city,String area) {
        System.err.println(province);
        System.err.println(city);
        if(city.equalsIgnoreCase("城市")){
            city = null ;
        }
        if(area.equalsIgnoreCase("地区")){
            area = null ;
        }
        System.err.println(area);
        System.err.println(this.service.getByPCA(province,city,area));
        return this.service.getByPCA(province,city,area);
    }





    @RequestMapping("index")
    public String index() {
        return "index";
    }
    @RequestMapping("list")
    public ModelAndView getSsList(HttpServletRequest request ){
        SplitPageUtil spu = new SplitPageUtil("雇员姓名:ename|联系电话:phone|是否离职(0：离职，1：在职):state", "/list") ;
        System.err.println("=========================");
        ModelAndView mav = new ModelAndView("files/serviceStationList");
        System.err.println(service);
        HashMap<String,Object> map= (HashMap<String,Object>)this.service.getSplit(spu.getCurrentPage(),spu.getLineSize(),null,null) ;
        mav.addAllObjects(map);
        return mav ;
    }
    @RequestMapping("del")
    public ModelAndView delete(int id,HttpServletRequest request ) {
        ModelAndView mav = new ModelAndView("redirect:/forward1");
        String msg = "删除成功，请继续操作" ;
        if(!this.service.delete(id)){
            msg = "删除失败，您删除的用户不存在或已经被删除";
        }
        mav.addObject("msg",msg);
        request.setAttribute("msg",msg);
        return mav;
    }
    @RequestMapping("forward1")
    public ModelAndView to(HttpServletRequest request, HttpServletResponse response,String msg){
        ModelAndView mav = new ModelAndView("files/back_forward");
        mav.addObject("msg",msg);
        request.setAttribute("msg",msg);
        mav.addObject("url","/list");
        return mav;
    }
    @RequestMapping("detail")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response,Long id){

        System.err.println(service);
        System.err.println(service);
        System.err.println(service);
        System.err.println(service);
        System.err.println(service);
        System.err.println(service);
        ModelAndView mav ;
        String msg = "操作成功";
        ServiceStation serviceStation = this.service.getById(id) ;
        if(serviceStation == null ){
            mav = new ModelAndView("redirect:list");
            msg = "用户不存在，请重新操作";
            mav.addObject("msg",msg );
        }
        mav = new ModelAndView("files/serviceStationDetail");
        mav.addObject("ss",serviceStation);
        System.err.println(serviceStation);
        return mav ;
    }
    @RequestMapping("edit_pre")
    public ModelAndView editPre(Long id,String userName){
        ModelAndView mav = new ModelAndView("files/serviceStationUp");
        mav.addObject("ss",this.service.getById(id));
        mav.addObject("id",id ) ;
        HttpSession session = getRequest().getSession();

        return mav ;
    }
    @RequestMapping("edit")
    public ModelAndView edit(ServiceStation ss){
        ModelAndView mav;
        String msg = "操作成功，请继续操作" ;
        Long ids = ss.getId();

        System.err.println();
        boolean flag = this.service.doEdit(ss);
        if(!flag){
            msg = "更新失败，请确认操作";
        }

        mav = new ModelAndView("redirect:forward1");
        mav.addObject("msg" ,msg );
        mav.addObject("url" ,"/list" );
        return mav;
    }
    @RequestMapping("add_pre")
    public ModelAndView addPre(Long id){
        ModelAndView mav = new ModelAndView("files/serviceStationAdd");

        mav.addObject("id",id ) ;

        return mav ;
    }
    @RequestMapping("add")
    public ModelAndView add(ServiceStation ss){
        ModelAndView mav;
        String msg = "操作成功，请继续操作" ;
        Long ids = ss.getId();
        boolean flag = this.service.doAdd(ss);
        if (!flag){
            mav = new ModelAndView("redirect:forward1");
            msg = "用户已经存在，请勿重复添加";
        }
        mav = new ModelAndView("redirect:forward1");
        mav.addObject("msg",msg);
        mav.addObject("url","/list");
        return mav;
    }

}

